package com.asa.rotatecarview

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.annotation.ColorInt
import androidx.core.graphics.withRotation
import androidx.core.graphics.withTranslation
import kotlin.math.atan2

/**
 * create By：anderson
 * on 2022/3/12
 * desc:
 */
class RotateCarView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private  val mCirclePaint = Paint().apply {
        isAntiAlias = true
        isDither = true
        style = Paint.Style.STROKE
    }
    private val mCarPaint= Paint().apply {

        isAntiAlias = true
        isDither = true
        style = Paint.Style.STROKE
        color = Color.GRAY
        strokeWidth = 4f
    }

    private var mCircleRadio = 0f

    private var distanceRatio = 0f

    private var mCircleColor :Int  = Color.BLACK

    private lateinit var mCarBitmap : Bitmap


    private val path = Path()
    private val pathMeasure = PathMeasure()
    private val pathMatrix = Matrix()

    private var withView = 0f
    private var heightView= 0f

    init {
        val res = context.obtainStyledAttributes(attrs,R.styleable.RotateCarView)
        mCircleColor = res.getColor(R.styleable.RotateCarView_circleColor,Color.BLACK)
        mCircleRadio = res.getDimension(R.styleable.RotateCarView_circleRadius,context.resources.getDimension(R.dimen.dp_3))
        res.recycle()

        mCirclePaint.strokeWidth = mCircleRadio
        mCirclePaint.color = mCircleColor

        mCarBitmap = BitmapFactory.decodeResource(context.resources,R.mipmap.icon_car)


        path.addCircle(0f,0f,200f,Path.Direction.CW)
        pathMeasure.setPath(path,false)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        this.withView = w.toFloat()
        this.heightView = h.toFloat()
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.also {
            pathMatrix.reset()
            //绘制圆形
            it.translate(withView/2,heightView/2)
            it.drawColor(Color.WHITE)
            //绘制小车
            distanceRatio += 0.0067f
            if (distanceRatio >1) {
                distanceRatio = 0f
            }
            val distance = pathMeasure.length * distanceRatio
            val pos = FloatArray(2) //记录位置
            val tan = FloatArray(2) //记录切点值xy
            pathMeasure.getPosTan(distance, pos, tan)
            val degree = atan2(tan[1].toDouble(), tan[0].toDouble()) * 180 / Math.PI    //计算出小车此时此刻的角度大小
            pathMatrix.postRotate(degree.toFloat(),mCarBitmap.width/2f,mCarBitmap.height/2f) // 计算出bitmap需要旋转的值
            pathMatrix.postTranslate(pos[0] - mCarBitmap.width/2,pos[1] - mCarBitmap.height/2) //计算出bitmap需要平移的值  这里平移和旋转的操作都是针对matrix 不会影响canvas
            it.drawPath(path,mCirclePaint)
            it.drawBitmap(mCarBitmap,pathMatrix,mCirclePaint)
            invalidate()
        }


    }

}